home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 022a / lyapun.zip / LYAP.C next >
Text File  |  1991-09-12  |  3KB  |  146 lines

  1. #include <rhalib.h>
  2. #include <graphics.h>
  3. #include <svga256.h>
  4.  
  5. char s[255];
  6.  
  7. int huge DetectVGA() {
  8.     int Vid;
  9.     printf("Choose a video mode:\n");
  10.     printf("  1)  VGA  [320  x 200]\n");
  11.     printf("  2)  SVGA [640  x 480]\n");
  12.     printf("  3)  SVGA [800  x 600]\n");
  13.     printf("  4)  SVGA [1024 x 768]\n");
  14.     gets(s);
  15.     if (sscanf(s, "%d", &Vid) < 1) Vid=VGA320x200;
  16.     return(Vid);
  17. }
  18.  
  19. void init() {
  20.     int Gd=DETECT, Gm;
  21.     installuserdriver("SVGA256",DetectVGA);
  22.     initgraph(&Gd,&Gm,getenv("BGIDIR"));
  23.     if (graphresult() != grOk) {
  24.                 printf("Unable to initialize graphics driver\n");
  25.             exit(1);
  26.     }
  27. }
  28.  
  29. void box(int, int, int, int);
  30.  
  31. void main() {
  32.     double rxy[2], x0, x1, x2, y1, y2, dx, dy, d, al;
  33.     int nx, ny, xmax, x, y, iter, niter, i, j, k, l;
  34.     
  35.     printf("Enter x dimension (default = 80): ");
  36.     gets(s);
  37.     if (sscanf(s, "%d", &nx) < 1) nx=80;
  38.     printf("Enter y dimension (default = 60): ");
  39.     gets(s);
  40.     if (sscanf(s, "%d", &ny) < 1) ny=60;
  41.  
  42.     printf("Enter window xmin (default -2.0): ");
  43.     gets(s);
  44.     if (sscanf(s, "%lf", &x1) < 1) x1 = -2.0;
  45.  
  46.     printf("Enter window xmax (default 2.0): ");
  47.     gets(s);
  48.     if (sscanf(s, "%lf", &x2) < 1) x2 = 2.0;
  49.  
  50.     printf("Enter window ymin (default -2.0): ");
  51.     gets(s);
  52.     if (sscanf(s, "%lf", &y1) < 1) y1 = -2.0;
  53.  
  54.     printf("Enter window ymax (default 2.0): ");
  55.     gets(s);
  56.     if (sscanf(s, "%lf", &y2) < 1) y2 = 2.0;
  57.  
  58.     printf("Enter iterations (default 100): ");
  59.     gets(s);
  60.     if (sscanf(s, "%d", &niter) < 1) niter = 100;
  61.  
  62.     printf("Enter bailout (default 100): ");
  63.     gets(s);
  64.     if (sscanf(s, "%d", &xmax) < 1) xmax = 100;
  65.  
  66.     init();
  67.  
  68. // Set parameter ranges
  69.     x0 = atan(1.0);
  70.  
  71. // Determine pixel steps
  72.     dx = (x2 - x1) / (double)nx;
  73.     dy = (y2 - y1) / (double)ny;
  74.  
  75. // Outline the screen
  76.     box (0, 0, nx-1, ny-1);
  77.  
  78. // Start iterating
  79. // This set up uses a 2 element array rxy[2] to do the periodic
  80. // forcing.  At each iteration, the MOD (%) function is used to
  81. // determine which value of r[] to use.
  82.     for (j = 0; j < ny; j++) {
  83.         rxy[0] = dy * (double)j + y1;
  84.         for (i = 0; i < nx; i++) {
  85.             rxy[1] = dx * (double)i + x1;
  86.             x = x0;
  87.             if (kbhit()) {
  88.                 closegraph();
  89.                 return;
  90.             }
  91.  
  92. // For some combinations, the iteration blows up.
  93. // Track and skip these.
  94.             for (iter = 0; iter < niter; iter++) {
  95.                 l = iter % 2;
  96.                 x = x * (rxy[l] - x);
  97.                 if (fabs(x) > (double)xmax) {
  98.                     k = 0;
  99.                     goto colorpix;
  100.                 }
  101.                 if (kbhit()) {
  102.                     closegraph();
  103.                     return;
  104.                 }
  105.             }
  106.             al = 0;
  107. // After the transients have died out, compute the average exponent.
  108.             for (iter = 0; iter < niter; iter++) {
  109.                 l = iter % 2;
  110.                 x = x * (rxy[l] - x);
  111.                 if (fabs(x) > (double)xmax) {
  112.                     k = 0;
  113.                     goto colorpix;
  114.                 }
  115.                 if (kbhit()) {
  116.                     closegraph();
  117.                     return;
  118.                 }
  119.                 d = fabs(rxy[l] - 2 * x);
  120.                 if (d == 0) d = .0000000001;
  121.                 al = al + log(d);
  122.             }
  123.             al = al / niter;
  124.  
  125. // If the exponent is >0 , color it black for chaotic orbits.
  126. // If not, then multiply the exponent by -100 & take the MOD 255 to
  127. // choose a color.
  128.             if (al > 0) k = 0;
  129.                 else k = (((int)al * (-100)) % 255) + 1;
  130.  
  131. colorpix:;        putpixel(i, j, k);
  132.      
  133.         }
  134.     }
  135.     getch();
  136.     closegraph();
  137. }
  138.  
  139. void box(int a, int b, int c, int d) {
  140.     setcolor(15);
  141.     line(a, b, c, b);
  142.     line(a, d, c, d);
  143.     line(a, b, a, d);
  144.     line(c, b, c, d);
  145. }
  146.